package com.server.Netty.handler;


import com.proto_java.*;
import com.proto_java.proto_main.ProtobufPackage;
import com.server.config.annotation.Event;
import com.server.config.annotation.WebSocketHandler;
import com.server.mongodb.domain.MGAddRequest;
import com.server.mongodb.domain.MGChatHistory;
import com.server.mongodb.domain.MGGroup;
import com.server.mongodb.domain.MGUser;
import com.server.mongodb.mapper.UserManageImpl;
import com.server.tools.Code;
import com.server.tools.JWTTools;
import com.server.tools.ProtoBufTools;
import com.server.tools.PublicUtils;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Slf4j
@Service
@WebSocketHandler
public class EventCallback {

    @Autowired
    UserManageImpl userManage;

    /**
     * 每个连接成功后都要执行一次它来将连接写入map中，断开时要删除该连接。
     * 这样方便对点发送数据和广播。
     * 并且同时将初始化需要的内容发给前端
     * @param chatInfo 里面只需要一个token
     * @param ctx 通道上下文
     */
    @Event(1)
    public void connectAndInit(ProtobufPackage.PackageInfo chatInfo, ChannelHandlerContext ctx){

        // 将取出的uid和连接对象写入集合
        Long userUid=JWTTools.getTokenUid(chatInfo.getToken()); // 对象的uid
        FrameDataHandler.allConnect.put(userUid,ctx);
        FrameDataHandler.connectTable.put(ctx,userUid);

        // 发送初始化数据包
        MGUser user=userManage.selectUser(userUid);
        List<PBAccountNumber.AccountNumber> friendList=new ArrayList<>();
        List<PBGroupInfo.GroupInfo> groupList=new ArrayList<>();

        List<MGAddRequest> addRequestFriend=userManage.getAddRequestFriend(userUid);
        List<MGAddRequest> addRequestGroup=userManage.getAddRequestGroup(userUid);
        List<PBAddGroupOrFriendRequest.AddGroupOrFriendRequest> addFriendList=new ArrayList<>();
        List<PBAddGroupOrFriendRequest.AddGroupOrFriendRequest> addGroupList=new ArrayList<>();

        // 打包好友请求消息
        for(MGAddRequest p : addRequestFriend){
            MGUser friend= userManage.selectUser(p.getSendUid());
            addFriendList.add(PBAddGroupOrFriendRequest.AddGroupOrFriendRequest.newBuilder()
                    .setUid(friend.getUid())
                    .setName(friend.getName())
                    .setImage(PublicUtils.getFile(friend.getHeadImage()))
                    .setIsRead(p.isRead()).build());
        }
        // 打包群加入请求消息
        for(MGAddRequest p : addRequestGroup){
            MGUser sendUser= userManage.selectUser(p.getSendUid());
            MGGroup group= userManage.selectGroup(p.getGetUid());
            addGroupList.add(PBAddGroupOrFriendRequest.AddGroupOrFriendRequest.newBuilder()
                    .setUid(sendUser.getUid())
                    .setName(sendUser.getName())
                    .setImage(PublicUtils.getFile(sendUser.getHeadImage()))
                    .setIsRead(p.isRead())
                    .setGroupUid(group.getUid())
                    .setGroupName(group.getName()).build());
        }
        // 打包好友列表
        for(Long p:user.getFriendUid()){
            MGUser friend= userManage.selectUserList(p);
            friendList.add(PBAccountNumber.AccountNumber.newBuilder()
                    .setUid(friend.getUid())
                    .setName(friend.getName())
                    .setImage(PublicUtils.getFile(friend.getHeadImage()))
                    .setExist(friend.isExist()).build());
        }
        // 打包群列表
        for(Long p:user.getGroupUid()){
            MGGroup group= userManage.selectGroupList(p);
            groupList.add(PBGroupInfo.GroupInfo.newBuilder()
                    .setUid(group.getUid())
                    .setName(group.getName())
                    .setGroupBossUid(group.getGroupBossUid())
                    .setHeadImage(PublicUtils.getFile(group.getHeadImage()))
                    .setExist(group.isExist()).build());
        }
        // 推送数据
        ctx.writeAndFlush(new BinaryWebSocketFrame(ProtoBufTools.getResponseClass(ProtobufPackage.PackageInfo.newBuilder()
                .setCallbackId(1)
                .setCode(Code.OK)
                .setWebSocketInit(PBWebSocketInit.WebSocketInit.newBuilder()
                        .addAllFriendList(friendList)
                        .addAllGroupList(groupList)
                        .addAllAddFriendList(addFriendList)
                        .addAllAddGroupList(addGroupList).build()).build())));
    }

    /**
     * 发送添加好友或群聊事件
     * @param chatInfo 数据包
     * @param ctx 通道上下文
     */
    @Event(2)
    public void addUserOrGroup(ProtobufPackage.PackageInfo chatInfo, ChannelHandlerContext ctx){

        // 根据判断结果取添加对象的uid
        boolean isGroup=chatInfo.hasGroupInfo();
        Long addUid=isGroup?chatInfo.getGroupInfo().getUid():chatInfo.getAccountNumber().getUid();

        // 解析token拿到发出添加请求的uid
        Long userUid=JWTTools.getTokenUid(chatInfo.getToken()); // 发出添加请求的uid

        // 如果已经是好友或者群成员，则直接返回
        if(userManage.isRepeat(addUid,userUid,isGroup)){
            ByteBuf byteBuf = ProtoBufTools.getResponseClass(Code.INSERT_ERR, "已经是好友或群成员了", chatInfo.getTaskId(),0);
            ctx.writeAndFlush(new BinaryWebSocketFrame(byteBuf));
            return;
        }
        // 如果还不是好友或者群成员，才插入数据
        // 插入失败则返回失败
        if(userManage.addRequest(addUid,userUid,chatInfo.hasGroupInfo())==null){
            ByteBuf byteBuf = ProtoBufTools.getResponseClass(Code.INSERT_ERR, "发送失败", chatInfo.getTaskId(),0);
            // 注意这里执行完writeAndFlush后会自动关闭byteBuf资源，所以不必手动关了
            ctx.writeAndFlush(new BinaryWebSocketFrame(byteBuf)); // 返回响应
            return;
        }

        // 如果对方在线，则直接将添加请求推送给对方
        ChannelHandlerContext pushEvent=FrameDataHandler.allConnect.get(
                // 如果是群则查出群主uid是否在线
                isGroup?userManage.selectGroup(addUid).getGroupBossUid():addUid);
        if(pushEvent!=null){
            ByteBuf byteBuf;
            MGUser user = userManage.selectUser(userUid);
            if(isGroup){ // 是否为群加入请求
                // 填充群请求内容
                MGGroup group = userManage.selectGroup(addUid);
                byteBuf =ProtoBufTools.getResponseClass(ProtobufPackage.PackageInfo.newBuilder()
                        .setCallbackId(3) // 不同的请求对应前端不同处理方法
                        .setCode(Code.OK)
                        .setAddGroupOrFriendRequest(PBAddGroupOrFriendRequest.AddGroupOrFriendRequest.newBuilder()
                                .setName(user.getName())
                                .setImage(PublicUtils.getFile(user.getHeadImage()))
                                .setUid(user.getUid())
                                .setIsRead(false)
                                .setGroupName(group.getName())
                                .setGroupUid(addUid).build()).build());
            }else{
                // 填充普通好友请求内容
                byteBuf =ProtoBufTools.getResponseClass(ProtobufPackage.PackageInfo.newBuilder()
                        .setCallbackId(2)
                        .setCode(Code.OK)
                        .setAddGroupOrFriendRequest(PBAddGroupOrFriendRequest.AddGroupOrFriendRequest.newBuilder()
                                .setName(user.getName())
                                .setImage(PublicUtils.getFile(user.getHeadImage()))
                                .setUid(user.getUid())
                                .setIsRead(false).build()).build());
            }
            // 发送给对方
            pushEvent.writeAndFlush(new BinaryWebSocketFrame(byteBuf)); // 返回响应
        }

        // 封装结果
        ByteBuf byteBuf = ProtoBufTools.getResponseClass(Code.OK, "发送成功", chatInfo.getTaskId(),0);
        // 注意这里执行完writeAndFlush后会自动关闭byteBuf资源，所以不必手动关了
        ctx.writeAndFlush(new BinaryWebSocketFrame(byteBuf)); // 返回响应
    }

    /**
     * 同意收到的添加好友或群聊请求
     * @param chatInfo 数据包
     * @param ctx 通道上下文
     */
    @Event(3)
    public void agreeAdd(ProtobufPackage.PackageInfo chatInfo,ChannelHandlerContext ctx){
        boolean isGroup=chatInfo.hasAddGroupOrFriendRequest();
        // 拿到发起请求者的uid
        Long userUid=isGroup?chatInfo.getAddGroupOrFriendRequest().getUid():chatInfo.getAccountNumber().getUid();
        // 接收请求者的uid
        Long addUid=isGroup?chatInfo.getAddGroupOrFriendRequest().getGroupUid():JWTTools.getTokenUid(chatInfo.getToken());

        boolean result=userManage.executeAddRequest(addUid,userUid);
        if(result){
            // 拿到发起者的连接对象,看一下是否在线
            ChannelHandlerContext pushEvent=FrameDataHandler.allConnect.get(userUid);
            if(isGroup){
                if(pushEvent!=null){
                    MGGroup group = userManage.selectGroup(addUid);
                    // 发送给自己,更新数据
                    pushEvent.writeAndFlush(new BinaryWebSocketFrame(ProtoBufTools.getResponseClass(
                            ProtobufPackage.PackageInfo.newBuilder()
                                    .setCallbackId(4)
                                    .setCode(Code.OK)
                                    .setGroupInfo(PBGroupInfo.GroupInfo.newBuilder()
                                            .setName(group.getName())
                                            .setHeadImage(PublicUtils.getFile(group.getHeadImage()))
                                            .setExist(group.isExist())
                                            .setGroupBossUid(group.getGroupBossUid())
                                            .setUid(group.getUid()).build()).build()))); // 返回响应
                }
            }else{
                // 拿到接收者的连接对象,他一定是在线的，毕竟是他发的这个请求
                ChannelHandlerContext requestEvent=FrameDataHandler.allConnect.get(addUid);
                // 拿到双方的信息
                MGUser sendUser = userManage.selectUser(addUid);
                MGUser getUser = userManage.selectUser(userUid);
                // 添加一条打招呼信息
                userManage.insertChatHistory(
                        PBChatHistory.ChatHistory.newBuilder()
                                .setData("你好呀,我已经同意了你的好友请求!")
                                .setIsImage(false)
                                .setSendUserUid(sendUser.getUid())
                                .setSendUserName(sendUser.getName())
                                .setSendUserHeadImage(PublicUtils.getFile(sendUser.getHeadImage()))
                                .setGetUserUid(getUser.getUid())
                                .setGetUserHeadImage(PublicUtils.getFile(getUser.getHeadImage()))
                                .setGetUserName(getUser.getName())
                                .setSendDate(String.valueOf(System.currentTimeMillis())).build(),false);
                // 给发这个同意请求的人推送打招呼信息
                requestEvent.writeAndFlush(new BinaryWebSocketFrame(ProtoBufTools.getResponseClass(
                        ProtobufPackage.PackageInfo.newBuilder()
                                .setCallbackId(5)
                                .setCode(Code.OK)
                                .setChatHistory(PBChatHistory.ChatHistory.newBuilder()
                                                .setData("你好呀,我已经同意了你的好友请求!")
                                                .setIsImage(false)
                                                .setSendUserUid(sendUser.getUid())
                                                .setSendUserName(sendUser.getName())
                                                .setSendUserHeadImage(PublicUtils.getFile(sendUser.getHeadImage()))
                                                .setGetUserUid(getUser.getUid())
                                                .setGetUserName(getUser.getName())
                                                .setGetUserHeadImage(PublicUtils.getFile(getUser.getHeadImage()))
                                                .setSendDate(PublicUtils.DateConvertString(
                                                        new Date(),"yyyy-MM-dd HH:mm:ss"))
                                        .build()).build()))); // 返回响应
                // 更新好友列表
                requestEvent.writeAndFlush(new BinaryWebSocketFrame(ProtoBufTools.getResponseClass(
                        ProtobufPackage.PackageInfo.newBuilder()
                                .setCallbackId(4)
                                .setCode(Code.OK)
                                .setAccountNumber(PBAccountNumber.AccountNumber.newBuilder()
                                        .setName(getUser.getName())
                                        .setImage(PublicUtils.getFile(getUser.getHeadImage()))
                                        .setExist(getUser.isExist())
                                        .setUid(getUser.getUid()).build()).build())));

                if(pushEvent!=null){
                    pushEvent.writeAndFlush(new BinaryWebSocketFrame(ProtoBufTools.getResponseClass(
                            ProtobufPackage.PackageInfo.newBuilder()
                                    .setCallbackId(5)
                                    .setCode(Code.OK)
                                    .setChatHistory(
                                            PBChatHistory.ChatHistory.newBuilder()
                                                    .setData("你好呀,我已经同意了你的好友请求!")
                                                    .setIsImage(false)
                                                    .setSendUserUid(sendUser.getUid())
                                                    .setSendUserName(sendUser.getName())
                                                    .setSendUserHeadImage(PublicUtils.getFile(sendUser.getHeadImage()))
                                                    .setGetUserUid(getUser.getUid())
                                                    .setGetUserName(getUser.getName())
                                                    .setGetUserHeadImage(PublicUtils.getFile(getUser.getHeadImage()))
                                                    .setSendDate(PublicUtils.DateConvertString(
                                                            new Date(),"yyyy-MM-dd HH:mm:ss"))
                                            .build()).build()))); // 返回响应

                    // 更新好友列表
                    pushEvent.writeAndFlush(new BinaryWebSocketFrame(ProtoBufTools.getResponseClass(
                            ProtobufPackage.PackageInfo.newBuilder()
                                    .setCallbackId(4)
                                    .setCode(Code.OK)
                                    .setAccountNumber(PBAccountNumber.AccountNumber.newBuilder()
                                            .setName(sendUser.getName())
                                            .setImage(PublicUtils.getFile(sendUser.getHeadImage()))
                                            .setExist(sendUser.isExist())
                                            .setUid(sendUser.getUid()).build()).build())));
                }
            }
            ctx.writeAndFlush(
                    new BinaryWebSocketFrame(
                            ProtoBufTools.getResponseClass(Code.OK, "添加成功", chatInfo.getTaskId(),0)));
            return;
        }
        ctx.writeAndFlush(
                new BinaryWebSocketFrame(
                        ProtoBufTools.getResponseClass(Code.INSERT_ERR, "添加失败", chatInfo.getTaskId(),0)));
    }

    /**
     * 解散群聊
     */
    @Event(4)
    public void disbandGroup(ProtobufPackage.PackageInfo chatInfo,ChannelHandlerContext ctx){
        PBAddGroupOrFriendRequest.AddGroupOrFriendRequest groupRequest =chatInfo.getAddGroupOrFriendRequest();
        if(userManage.disbandGroup(groupRequest.getUid(),groupRequest.getGroupUid())){
            ctx.writeAndFlush(
                    new BinaryWebSocketFrame(
                            ProtoBufTools.getResponseClass(Code.OK, "操作成功!", chatInfo.getTaskId(),0)));
        }else{
            ctx.writeAndFlush(
                    new BinaryWebSocketFrame(
                            ProtoBufTools.getResponseClass(Code.DELETE_ERR, "操作失败", chatInfo.getTaskId(),0)));
        }
    }

    /**
     * 删除好友
     */
    @Event(5)
    public void deleteFriend(ProtobufPackage.PackageInfo chatInfo,ChannelHandlerContext ctx){
        PBAccountNumber.AccountNumber friend =chatInfo.getAccountNumber();
        Long userUid =JWTTools.getTokenUid(chatInfo.getToken());
        if(userManage.disbandFriend(friend.getUid(),userUid)){
            ctx.writeAndFlush(
                    new BinaryWebSocketFrame(
                            ProtoBufTools.getResponseClass(Code.OK, "操作成功!", chatInfo.getTaskId(),0)));
        }else{
            ctx.writeAndFlush(
                    new BinaryWebSocketFrame(
                            ProtoBufTools.getResponseClass(Code.DELETE_ERR, "操作失败", chatInfo.getTaskId(),0)));
        }
    }

    /**
     * 删除好友或群的添加请求
     */
    @Event(6)
    public void deleteAddRequest(ProtobufPackage.PackageInfo chatInfo,ChannelHandlerContext ctx){

        // 根据判断结果取到发出添加请求的uid
        boolean isGroup=chatInfo.hasAddGroupOrFriendRequest();
        Long sendUid=isGroup?chatInfo.getAddGroupOrFriendRequest().getUid():chatInfo.getAccountNumber().getUid();
        // 被添加的uid
        Long getUid=isGroup?chatInfo.getAddGroupOrFriendRequest().getGroupUid():JWTTools.getTokenUid(chatInfo.getToken());

        if(userManage.deleteAddRequest(sendUid,getUid)){
            ctx.writeAndFlush(
                    new BinaryWebSocketFrame(
                            ProtoBufTools.getResponseClass(Code.OK, "操作成功!", chatInfo.getTaskId(),0)));
        }else{
            ctx.writeAndFlush(
                    new BinaryWebSocketFrame(
                            ProtoBufTools.getResponseClass(Code.DELETE_ERR, "操作失败", chatInfo.getTaskId(),0)));
        }
    }

    /**
     * 标记已读好友或群的添加请求
     * 不需要返回什么
     */
    @Event(7)
    public void readAlreadyAddRequest(ProtobufPackage.PackageInfo chatInfo,ChannelHandlerContext ctx){
        userManage.readAlreadyAddRequest(chatInfo.getAddGroupOrFriendRequest().getUid(),
                chatInfo.getAddGroupOrFriendRequest().getIsGroup());
        // 如果出现异常就保存日志之类的，这里就不写了
    }

    /**
     * 处理保存客户端发来的消息
     */
    @Event(8)
    public void saveMsg(ProtobufPackage.PackageInfo chatInfo,ChannelHandlerContext ctx){

        boolean isGroup=chatInfo.getChatHistory().getGroupUid()>0;
        PBChatHistory.ChatHistory chatHistory=chatInfo.getChatHistory();

        boolean exist=userManage.insertChatHistory(chatHistory,isGroup);
        // 检查一下是否存在正常关系
        if(!exist){
            ctx.writeAndFlush(
                    new BinaryWebSocketFrame(
                            ProtoBufTools.getResponseClass(Code.INSERT_ERR, "请先加对方为好友,或对方/群已注销", chatInfo.getTaskId(),0)));
            return;
        }
        // 准备一下群的数据容器
        MGGroup mgGroup=null;
        // 获取发送者头像
        MGUser user=userManage.selectUser(chatHistory.getSendUserUid());
        // 如果是群则需要查询每个群成员是否在线
        if(isGroup){
            mgGroup=userManage.selectGroup(chatHistory.getGroupUid());
            List<Long> memberUid = mgGroup.getMemberUid();
            for(Long p : memberUid){
                if(chatHistory.getSendUserUid()==p){
                    continue;
                }
                ChannelHandlerContext pushEvent=FrameDataHandler.allConnect.get(p);
                if(pushEvent!=null){
                    ByteBuf byteBuf =ProtoBufTools.getResponseClass(ProtobufPackage.PackageInfo.newBuilder()
                            .setCallbackId(5) // 不同的请求对应前端不同处理方法
                            .setChatHistory(PBChatHistory.ChatHistory.newBuilder()
                                    .setData(chatHistory.getData())
                                    .setIsImage(chatHistory.getIsImage())
                                    .setGroupUid(chatHistory.getGroupUid())
                                    .setGroupHeadImage(PublicUtils.getFile(mgGroup.getHeadImage()))
                                    .setGroupName(chatHistory.getGroupName())
                                    .setSendUserUid(chatHistory.getSendUserUid())
                                    .setSendUserName(chatHistory.getSendUserName())
                                    .setSendUserHeadImage(PublicUtils.getFile(user.getHeadImage()))
                                    .build()).build());
                    // 发送给对方
                    pushEvent.writeAndFlush(new BinaryWebSocketFrame(byteBuf)); // 返回响应
                }
            }
        }else{ // 不是群的话就直接查人
            // 如果对方在线，则直接将添加请求推送给对方
            ChannelHandlerContext pushEvent=FrameDataHandler.allConnect.get(chatHistory.getGetUserUid());
            if(pushEvent!=null){
                ByteBuf byteBuf =ProtoBufTools.getResponseClass(ProtobufPackage.PackageInfo.newBuilder()
                        .setCallbackId(5) // 不同的请求对应前端不同处理方法
                        .setChatHistory(PBChatHistory.ChatHistory.newBuilder()
                                .setData(chatHistory.getData())
                                .setIsImage(chatHistory.getIsImage())
                                .setGetUserUid(chatHistory.getGetUserUid())
                                .setSendUserUid(chatHistory.getSendUserUid())
                                .setSendUserHeadImage(PublicUtils.getFile(user.getHeadImage()))
                                .setSendUserName(chatHistory.getSendUserName()).build()).build());
                // 发送给对方
                pushEvent.writeAndFlush(new BinaryWebSocketFrame(byteBuf)); // 返回响应
            }
        }
        // 获取接收者头像
        MGUser getUser=userManage.selectUser(chatHistory.getGetUserUid());
        ctx.writeAndFlush(new BinaryWebSocketFrame(ProtoBufTools.getResponseClass(ProtobufPackage.PackageInfo.newBuilder()
                .setCode(Code.OK)
                .setTaskId(chatInfo.getTaskId())
                .setChatHistory(PBChatHistory.ChatHistory.newBuilder()
                        .setData(chatHistory.getData())
                        .setIsImage(chatHistory.getIsImage())
                        .setGroupUid(chatHistory.getGroupUid())
                        .setGroupHeadImage(PublicUtils.getFile(mgGroup==null?null:mgGroup.getHeadImage()))
                        .setGroupName(chatHistory.getGroupName())
                        .setGetUserUid(chatHistory.getGetUserUid())
                        .setGetUserHeadImage(PublicUtils.getFile(getUser==null?null:getUser.getHeadImage()))
                        .setGetUserName(chatHistory.getGetUserName())
                        .setSendUserUid(chatHistory.getSendUserUid())
                        .setSendUserName(chatHistory.getSendUserName()).build()).build())));
    }

    /**
     * 聊天消息已读处理
     * 不需要返回什么
     */
    @Event(9)
    public void readMsg(ProtobufPackage.PackageInfo chatInfo,ChannelHandlerContext ctx){
        long uid ; // 发送消息对象或群的uid
        boolean isGroup=chatInfo.getChatHistory().getGroupUid()>0;
        // 根据是否为群做对应取值
        if(isGroup){
            uid=chatInfo.getChatHistory().getGroupUid();
        }else{
            uid=chatInfo.getChatHistory().getSendUserUid();
        }
        userManage.readAlreadyChatHistory(uid,JWTTools.getTokenUid(chatInfo.getToken()),isGroup);
    }

    /**
     * 获取对应uid的所有聊天消息
     */
    @Event(10)
    public void getChatHistory(ProtobufPackage.PackageInfo chatInfo,ChannelHandlerContext ctx){

        PBGetChatHistory.GetChatHistory getChatHistory = chatInfo.getGetChatHistory();
        List<MGChatHistory> data=userManage.getChatHistory(getChatHistory);
        List<PBChatHistory.ChatHistory> lastData=new ArrayList<>();
        // 装填所有数据
        if(chatInfo.getGetChatHistory().getIsGroup()){
            for (MGChatHistory p : data) {
                lastData.add(PBChatHistory.ChatHistory.newBuilder()
                        .setData(p.isImage()?PublicUtils.getFile(p.getData()):p.getData())
                        .setIsImage(p.isImage())
                        .setSendUserUid(p.getSendUserUid())
                        .setSendUserHeadImage(PublicUtils.getFile(p.getSendUserHeadImage()))
                        .setSendUserName(p.getSendUserName())
                        .setSendDate(PublicUtils.DateConvertString(p.getSendDate(),"yyyy-MM-dd HH:mm:ss"))
                        .setGroupUid(p.getGroupUid())
                        .setGroupName(p.getGroupName())
                        .setGroupHeadImage(PublicUtils.getFile(p.getGroupHeadImage())).build());
            }
        }else{
            for (MGChatHistory p : data) {
                lastData.add(PBChatHistory.ChatHistory.newBuilder()
                        .setData(p.isImage()?PublicUtils.getFile(p.getData()):p.getData())
                        .setIsImage(p.isImage())
                        .setSendUserUid(p.getSendUserUid())
                        .setSendUserHeadImage(PublicUtils.getFile(p.getSendUserHeadImage()))
                        .setSendUserName(p.getSendUserName())
                        .setGetUserName(p.getGetUserName())
                        .setGetUserUid(p.getGetUserUid())
                        .setGetUserHeadImage(PublicUtils.getFile(p.getGetUserHeadImage()))
                        .setSendDate(PublicUtils.DateConvertString(p.getSendDate(),"yyyy-MM-dd HH:mm:ss")).build());
            }
        }

        ctx.writeAndFlush(new BinaryWebSocketFrame(ProtoBufTools.getResponseClass(
                ProtobufPackage.PackageInfo.newBuilder()
                        .setCallbackId(6)
                        .setCode(Code.OK)
                        .setGetChatHistory(PBGetChatHistory.GetChatHistory.newBuilder()
                                .addAllChatHistory(lastData).build()).build())));
    }

    /**
     * 退出群聊
     */
    @Event(11)
    public void exitGroup(ProtobufPackage.PackageInfo chatInfo,ChannelHandlerContext ctx){
        PBAddGroupOrFriendRequest.AddGroupOrFriendRequest groupRequest =chatInfo.getAddGroupOrFriendRequest();
        if(userManage.exitGroup(groupRequest.getUid(),groupRequest.getGroupUid())){
            ctx.writeAndFlush(
                    new BinaryWebSocketFrame(
                            ProtoBufTools.getResponseClass(Code.OK, "操作成功!", chatInfo.getTaskId(),0)));
        }else{
            ctx.writeAndFlush(
                    new BinaryWebSocketFrame(
                            ProtoBufTools.getResponseClass(Code.DELETE_ERR, "操作失败", chatInfo.getTaskId(),0)));
        }
    }

}
